package net.kindroid.fm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class RequireHeadSetView extends Activity
{
	/**
	 * ok button
	 */
	private Button bt_ok = null;

	/**
	 * poll headset is on
	 */
	private final static int POLLINGHEADSET = 1;

	/**
	 * judge current view is over
	 */
	private boolean isOver = false;

	/**
	 * 10 seconds listener
	 */
	private int totalAlarm = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.requireheadlayout);

		bt_ok = (Button) this.findViewById(R.id.requireheadset_button_Ok);
		bt_ok.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				isOver = true;
				if (mHandler != null)
				{
					mHandler.removeMessages(POLLINGHEADSET);
				}
				Intent close_intent = new Intent();
				close_intent.setAction("android.intent.action.CLOSE_FM");
				sendBroadcast(close_intent);
				RequireHeadSetView.this.finish();
			}
		});

		isOver = false;
		mHandler.sendEmptyMessage(POLLINGHEADSET);
	}

	/**
	 * operator event looper
	 */
	Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			switch (msg.what)
			{
				case POLLINGHEADSET:
				{
					if (!isOver && totalAlarm <= 20)
					{
						if (isWiredHeadsetOn())
						{
							Intent intent = new Intent(RequireHeadSetView.this, FMEntryView.class);
							RequireHeadSetView.this.startActivity(intent);
						} else
						{
							totalAlarm++;
							mHandler.sendEmptyMessageDelayed(POLLINGHEADSET, 500);
						}
					} else
					{
						RequireHeadSetView.this.finish();
					}
					break;
				}
			}
		}

	};

	@Override
	protected void onRestart()
	{
		super.onRestart();
	}

	@Override
	protected void onStart()
	{
		super.onStart();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		super.onPause();

		// press home or goto FM
		isOver = true;
		mHandler.removeMessages(POLLINGHEADSET);
		Intent close_intent = new Intent();
		close_intent.setAction("android.intent.action.CLOSE_FM");
		sendBroadcast(close_intent);
		RequireHeadSetView.this.finish();
	}

	@Override
	protected void onStop()
	{
		release();
		super.onStop();
	}

	@Override
	protected void onDestroy()
	{
		release();
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// if is back key
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			isOver = true;
			mHandler.removeMessages(POLLINGHEADSET);
			Intent close_intent = new Intent();
			close_intent.setAction("android.intent.action.CLOSE_FM");
			sendBroadcast(close_intent);
			RequireHeadSetView.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * judge current head set is on
	 * 
	 * @return
	 */
	private boolean isWiredHeadsetOn()
	{
		boolean result = false;
		AudioManager localAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		if (localAudioManager != null && localAudioManager.isWiredHeadsetOn())
		{
			result = true;
		}

		return result;
	}

	/**
	 * when back will release resource
	 */
	private void release()
	{
		bt_ok = null;
		mHandler = null;
	}
}
